#!/usr/bin/env python3
"""
分析浏览器实际的POST请求数据
"""

import json
import urllib.parse

def analyze_browser_request():
    """分析浏览器请求数据"""
    
    print("🔍 分析浏览器实际的POST请求数据...")
    
    # 从拦截到的数据中获取POST请求
    with open('word_network_data.json', 'r', encoding='utf-8') as f:
        network_data = json.load(f)
    
    if not network_data:
        print("❌ 没有找到网络数据")
        return
    
    request_data = network_data[0]['request_data']
    print(f"原始POST数据: {request_data}")
    
    # 解析POST数据
    parsed_data = urllib.parse.parse_qs(request_data)
    
    print(f"\n📋 解析后的POST参数:")
    for key, value in parsed_data.items():
        print(f"  {key}: {value[0] if value else ''}")
    
    # 解析ParamsFore
    params_fore_encoded = parsed_data.get('ParamsFore', [''])[0]
    print(f"\n🔍 ParamsFore (编码): {params_fore_encoded}")
    
    # 双重解码
    params_fore_decoded = urllib.parse.unquote(urllib.parse.unquote(params_fore_encoded))
    print(f"🔍 ParamsFore (解码): {params_fore_decoded}")
    
    # 解析JSON
    try:
        params_fore_json = json.loads(params_fore_decoded)
        print(f"🔍 ParamsFore (JSON): {json.dumps(params_fore_json, indent=2, ensure_ascii=False)}")
    except Exception as e:
        print(f"❌ 解析ParamsFore JSON失败: {e}")
    
    # 解析CommonJsonPara
    common_json_encoded = parsed_data.get('CommonJsonPara', [''])[0]
    print(f"\n🔍 CommonJsonPara (编码): {common_json_encoded}")
    
    # 单重解码
    common_json_decoded = urllib.parse.unquote(common_json_encoded)
    print(f"🔍 CommonJsonPara (解码): {common_json_decoded}")
    
    # 解析JSON
    try:
        common_json_json = json.loads(common_json_decoded)
        print(f"🔍 CommonJsonPara (JSON): {json.dumps(common_json_json, indent=2, ensure_ascii=False)}")
    except Exception as e:
        print(f"❌ 解析CommonJsonPara JSON失败: {e}")
    
    # 解析ParamsPageInfo
    params_page_encoded = parsed_data.get('ParamsPageInfo', [''])[0]
    print(f"\n🔍 ParamsPageInfo (编码): {params_page_encoded}")
    
    # 双重解码
    params_page_decoded = urllib.parse.unquote(urllib.parse.unquote(params_page_encoded))
    print(f"🔍 ParamsPageInfo (解码): {params_page_decoded}")
    
    # 解析JSON
    try:
        params_page_json = json.loads(params_page_decoded)
        print(f"🔍 ParamsPageInfo (JSON): {json.dumps(params_page_json, indent=2, ensure_ascii=False)}")
    except Exception as e:
        print(f"❌ 解析ParamsPageInfo JSON失败: {e}")
    
    print(f"\n📊 总结:")
    print(f"  URL: {network_data[0]['url']}")
    print(f"  编码方式: ParamsFore和ParamsPageInfo使用双重URL编码，CommonJsonPara使用单重URL编码")
    print(f"  JSON格式: 使用紧凑格式（无空格）")

if __name__ == "__main__":
    analyze_browser_request()
